package com.j2205.hongzhiyuan.service.impl;

import com.j2205.hongzhiyuan.mapper.ChangCiMapper;
import com.j2205.hongzhiyuan.pojo.ChangCiInfo;
import com.j2205.hongzhiyuan.service.ChangCiService;
import com.j2205.hongzhiyuan.util.MybatisUtil;
import com.j2205.hongzhiyuan.vo.VoChangCi;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class ChangCiServiceImpl implements ChangCiService {

    @Override
    public boolean addChangci(ChangCiInfo info) {
        ChangCiMapper changCiMapper= MybatisUtil.getSession().getMapper(ChangCiMapper.class);
        int i= changCiMapper.addChangCi(info);
        if(i>-1){
            MybatisUtil.getSession().commit();
            MybatisUtil.clseAll();
            return true;
        }
        return false;
    }

    @Override
    public List<VoChangCi> findAll() {
        ChangCiMapper changCiMapper = MybatisUtil.getSession().getMapper(ChangCiMapper.class);
        List<ChangCiInfo> all = changCiMapper.findAll();
//        MybatisUtil.clseAll();
        List<VoChangCi> ccs = new ArrayList<>();
        for (ChangCiInfo info : all) {
            VoChangCi vo = new VoChangCi();
            vo.setCgID(info.getCgID());
            vo.setMaxNumber(info.getMaxNumber());
            vo.setStatus(info.getStatus());
            vo.setMaxOrderNumber(info.getMaxOrderNumber());
            String opendate = new SimpleDateFormat("yyyy-MM-dd").format(info.getOpenDate());
            vo.setOpenDate(opendate);
            String startTime = new SimpleDateFormat("HH:mm:ss").format(info.getStartTime());
            vo.setStartTime(startTime);
            String endTime = new SimpleDateFormat("HH:mm:ss").format(info.getEndTime());
            vo.setEndTime(endTime);
            ccs.add(vo);
        }
        return ccs;
    }

    @Override
    public VoChangCi findById(Integer id) {
        ChangCiMapper changCiMapper=MybatisUtil.getSession().getMapper(ChangCiMapper.class);
        ChangCiInfo info = changCiMapper.findById(id);
        VoChangCi voChangCi = new VoChangCi();
        voChangCi.setCgID(info.getCgID());
        voChangCi.setMaxNumber(info.getMaxNumber());
        voChangCi.setStatus(info.getStatus());
        voChangCi.setMaxOrderNumber(info.getMaxOrderNumber());
        String opendate = new SimpleDateFormat("yyyy-MM-dd").format(info.getOpenDate());
        voChangCi.setOpenDate(opendate);
        String startTime = new SimpleDateFormat("HH:mm:ss").format(info.getStartTime());
        voChangCi.setStartTime(startTime);
        String endTime = new SimpleDateFormat("HH:mm:ss").format(info.getEndTime());
        voChangCi.setEndTime(endTime);
        MybatisUtil.clseAll();//释放资源（连接）
        return voChangCi;
    }

    @Override
    public boolean update(ChangCiInfo changCiInfo) {
        ChangCiMapper changCiMapper=MybatisUtil.getSession().getMapper(ChangCiMapper.class);
        int i=changCiMapper.update(changCiInfo);
        if(i>-1){
            MybatisUtil.commit();
            MybatisUtil.clseAll();
            return true;
        }
        return false;
    }

    @Override
    public boolean plshenHe(List<Integer> ids1) {
        ChangCiMapper mapper = MybatisUtil.getSession().getMapper(ChangCiMapper.class);
        int i = mapper.plshenHe(ids1);
        if (i>-1){
            MybatisUtil.commit();
            MybatisUtil.clseAll();
            return true;
        }
        return false;
    }
    @Override
    public List<VoChangCi> select(String openDate, String status) {
        if(status.equals("0")){
            status = "已审核";
        }else{
            status = "未审核";
        }
        ChangCiMapper changCiMapper=MybatisUtil.getSession().getMapper(ChangCiMapper.class);
        List<ChangCiInfo> changCiInfos=changCiMapper.select(openDate, status);
        List<VoChangCi> voChangCis=new ArrayList<>();
        for(ChangCiInfo info:changCiInfos){
            VoChangCi voChangCi=new VoChangCi();
            voChangCi.setCgID(info.getCgID());
            voChangCi.setMaxNumber(info.getMaxNumber());
            voChangCi.setStatus(info.getStatus());
            voChangCi.setMaxOrderNumber(info.getMaxOrderNumber());
            String opendate =new SimpleDateFormat("yyyy-MM-dd").format( info.getOpenDate());
            voChangCi.setOpenDate(opendate);
            String startTime =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( info.getStartTime());
            voChangCi.setStartTime(startTime);
            String endTime =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( info.getEndTime());
            voChangCi.setEndTime(endTime);
            voChangCis.add(voChangCi);
        }
        return voChangCis;
    }

    @Override
    public boolean deleteById(Integer id) {
        ChangCiMapper changCiMapper=MybatisUtil.getSession().getMapper(ChangCiMapper.class);
        int i=changCiMapper.deleteById(id);
        if(i>-1){
            MybatisUtil.commit();
            MybatisUtil.clseAll();
            return true;

        }
        return false;
    }

    @Override
    public boolean auditById(Integer id) {
        ChangCiMapper changCiMapper=MybatisUtil.getSession().getMapper(ChangCiMapper.class);
        int i=changCiMapper.auditById(id);
        if(i>-1){
            MybatisUtil.commit();
            MybatisUtil.clseAll();
            return true;

        }
        return false;
    }
}
